//
//  ViewController2.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UISlider *redSlider;
@property (weak, nonatomic) IBOutlet UISlider *greenSlider;
@property (weak, nonatomic) IBOutlet UISlider *blueSlider;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateInfo];
    // 监听程序进入前台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    self.view1.backgroundColor = [UIColor colorWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)updateInfo{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    self.redSlider.value = [d floatForKey:@"redslider"];
    self.greenSlider.value = [d floatForKey:@"greenslider"];
    self.blueSlider.value = [d floatForKey:@"blueslider"];
}
- (IBAction)sliderVauleChanged:(UISlider *)sender forEvent:(UIEvent *)event {
    self.view1.backgroundColor = [UIColor colorWithRed:self.redSlider.value/255.0 green:self.greenSlider.value/255.0 blue:self.blueSlider.value/255.0 alpha:1];
}


- (IBAction)goBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)redSliderVauleChanged:(UISlider *)sender {
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    
    [d setFloat:sender.value forKey:@"redslider"];
    [d synchronize];
    
}
- (IBAction)greenSliderVauleChanged:(UISlider *)sender {
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:sender.value forKey:@"greenslider"];
    [d synchronize];
}
- (IBAction)blueSliderVauleChanged:(UISlider *)sender {
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:sender.value forKey:@"blueslider"];
    [d synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
